#include "Uses.h"
#include <set>

Uses::Uses(){
	this->usesStmtIndex = vector<pair<int, int>>();
	this->usesProcIndex = vector<pair<int, int>>();
}

int Uses::addUsesStatement(int stmtNum, int varIndex){
	if(stmtNum <= 0 || varIndex < 0){
		return -1;
	}
	this->usesStmtIndex.push_back(make_pair(stmtNum, varIndex));
	return usesStmtIndex.size()-1;
}

int Uses::addUsesProcedure(int procIndex, int varIndex){
	if(procIndex < 0 || varIndex < 0){
		return -1;
	}
	this->usesProcIndex.push_back(make_pair(procIndex, varIndex));
	return usesProcIndex.size()-1;
}

vector<pair<int, int>> Uses::getUsesStatement(int stmtNumber, int varIndex){
	/* current: cater for assignment and container statements only.
	 *			(for procedures, use getUsesProcedure() instead)
	 * future: cater for call statements.
	 */
	set<pair<int, int>>* matchingPairs = new set<pair<int, int>>();
	for(vector<pair<int, int>>::iterator it = usesStmtIndex.begin(); it != usesStmtIndex.end(); it++) {
		if((it->first == stmtNumber || stmtNumber == ANYVALUE) && 
			(it->second == varIndex || varIndex == ANYVALUE)){
				matchingPairs->insert(*it);
		}
	}
	return vector<pair<int, int>>(matchingPairs->begin(), matchingPairs->end());
}

vector<pair<int, int>> Uses::getUsesProcedure(int procIndex, int varIndex){
	/* current: cater for directly procedure only.
	 * future: cater for transitive procedure evaluation.
	 */
	set<pair<int, int>>* matchingPairs = new set<pair<int, int>>();
	for(vector<pair<int, int>>::iterator it = usesProcIndex.begin(); it != usesProcIndex.end(); it++) {
		if((it->first == procIndex || procIndex == ANYVALUE) &&
			(it->second == varIndex || varIndex == ANYVALUE)){
				matchingPairs->insert(*it);
		}
	}
	return vector<pair<int, int>>(matchingPairs->begin(), matchingPairs->end());
}